package com.cncf.controller.manage;

import com.cncf.entity.Enterprise;
import com.cncf.response.ResponseData;
import com.cncf.service.EnterpriseService;
import com.cncf.util.JedisUtil;
import com.cncf.util.ObjectAndByte;
import com.cncf.util.RandCharsUtils;
import com.cncf.util.Util;
import com.cncf.vo.LoginVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zj
 */
@Api(value = "Manage", description = "管理企业的相关接口")
@Controller
@RequestMapping("/manage/enterprise")
public class ManageEnterpriseController {
    private static final Logger logger = LoggerFactory.getLogger(ManageEnterpriseController.class);

    @Autowired
    private EnterpriseService enterpriseService;

    @ApiOperation(value = "为企业创建内部账号和公用账号", notes = "")
    @RequestMapping(value = "createEnterpriseAccount", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<List<Enterprise>> createEnterpriseAccount(
            @ApiParam("企业id列表") @RequestParam(value = "ids") List<String> ids) {

        ResponseData<List<Enterprise>> responseData = new ResponseData<>();
        int count = 0;
        for (String id : ids) {
            Enterprise enterprise = new Enterprise();
            enterprise.setId(id);
            String password = RandCharsUtils.getRandomString(6);
            enterprise.setPassword(password);
            password = RandCharsUtils.getRandomString(6);
            enterprise.setInnerPassword(password);
            int res = enterpriseService.updateEnterprise(enterprise);
            if (res == 0) {
                responseData.jsonFill(2, "创建id为：" + id + "的企业账号失败，在其之前成功创建了" + count + "个企业账号", null);
                return responseData;
            }
            count += res;
        }
        List<Enterprise> enterpriseList = enterpriseService.getEnterpriseByIdList(ids);
        responseData.jsonFill(1, null, enterpriseList);
        return responseData;
    }

    @ApiOperation(value = "更新企业", notes = "")
    @RequestMapping(value = "updateEnterprise", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<Boolean> updateEnterprise(
            @ApiParam("") @RequestParam("id") String id,
            @ApiParam("企业名") @RequestParam(value = "enterpriseName", required = false) String enterpriseName,
            @ApiParam("曾用名") @RequestParam(value = "oldName", required = false) String oldName,
            @ApiParam("地区") @RequestParam(value = "area", required = false) String area,
            @ApiParam("邮编") @RequestParam(value = "postalcode", required = false) String postalcode,
            @ApiParam("电话") @RequestParam(value = "tel", required = false) String tel,
            @ApiParam("外部密码") @RequestParam(value = "password", required = false) String password,
            @ApiParam("内部密码") @RequestParam(value = "innerPassword", required = false) String innerPassword,
            @ApiParam("余额") @RequestParam(value = "balance", required = false) Double balance) {
        ResponseData<Boolean> responseData = new ResponseData<>();
        Enterprise enterprise = new Enterprise();
        enterprise.setId(id);
        enterprise.setEnterpriseName(enterpriseName);
        enterprise.setOldName(oldName);
        enterprise.setArea(area);
        enterprise.setPostalcode(postalcode);
        enterprise.setTel(tel);
        enterprise.setPassword(password);
        enterprise.setInnerPassword(innerPassword);
        enterprise.setBalance(balance);
        int res = enterpriseService.updateEnterprise(enterprise);
        if (res == 0) {
            responseData.jsonFill(2, "更新失败", false);
            return responseData;
        }
        responseData.jsonFill(1, null, true);
        return responseData;
    }

    @ApiOperation(value = "通过ID获取企业", notes = "")
    @RequestMapping(value = "getEnterpriseById", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<Enterprise> getEnterpriseById(@ApiParam("") @RequestParam("id") String id) {
        ResponseData<Enterprise> responseData = new ResponseData<>();
        Enterprise enterprise = enterpriseService.getEnterpriseById(id);
        if (enterprise == null) {
            responseData.jsonFill(2, "无效的id", null);
            return responseData;
        }
        responseData.jsonFill(1, null, enterprise);
        return responseData;
    }

    @ApiOperation(value = "获取所有的企业", notes = "不提供page和pageSize就不分页")
    @RequestMapping(value = "getAllEnterprise", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<Enterprise>> getAllEnterprise(
            @ApiParam("page") @RequestParam(value = "page", required = false) Integer page,
            @ApiParam("pageSize") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        ResponseData<List<Enterprise>> responseData = new ResponseData<>();
        List<Enterprise> list = enterpriseService.getAllEnterprise(page, pageSize);
        responseData.jsonFill(1, null, list);
        responseData.setCount(list.size());
        return responseData;
    }

    @ApiOperation(value = "获取所有没有账户企业", notes = "不提供page和pageSize就不分页")
    @RequestMapping(value = "getAllNewEnterprise", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<Enterprise>> getAllNewEnterprise(
            @ApiParam("page") @RequestParam(value = "page", required = false) Integer page,
            @ApiParam("pageSize") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        ResponseData<List<Enterprise>> responseData = new ResponseData<>();
        List<Enterprise> list = enterpriseService.getAllNewEnterprise(page, pageSize);
        responseData.jsonFill(1, null, list);
        responseData.setCount(list.size());
        return responseData;
    }


    @ApiOperation(value = "删除", notes = "")
    @RequestMapping(value = "deleteEnterprise/{id}", method = {RequestMethod.DELETE})
    @ResponseBody
    public ResponseData<Boolean> deleteEnterprise(@ApiParam("ID") @PathVariable String id) {
        ResponseData<Boolean> responseData = new ResponseData<>();
        int res = enterpriseService.deleteEnterprise(id);
        if (res == 0) {
            responseData.jsonFill(2, "删除失败", false);
            return responseData;
        }
        responseData.jsonFill(1, "删除成功", true);
        return responseData;
    }

}
